/**
* Author: Samfu
* Time  : 下午3:09:01
* Desc  : 
*/
package com.fu.moneybag.controller;

import java.io.UnsupportedEncodingException;
<<<<<<< HEAD
import java.util.ArrayList;
=======
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fu.moneybag.entity.Article;
import com.fu.moneybag.entity.ArticleType;
import com.fu.moneybag.entity.User;
import com.fu.moneybag.service.ArticleService;
import com.fu.moneybag.service.ArticleTypeService;
import com.fu.moneybag.service.UserService;
<<<<<<< HEAD
import com.fu.moneybag.utils.Page;
=======
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a

@Controller
@RequestMapping("/article")
public class ArticleController {

	private Logger logger = Logger.getLogger(ArticleController.class);

	@Autowired
	ArticleService articleService;
	
	@Autowired
	ArticleTypeService articleTypeService;

	@Autowired
	UserService userService;

	// 显示article添加页面
	@RequestMapping("/addArticle")
	public String showAddArticle(HttpServletRequest request, HttpSession session) {
		// check if session valid, need to use a common checking as so many
		// places use it
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/user/login";//session expired
		}
		//显示在下拉框的文章类型
		List<ArticleType> articleTypes = articleTypeService.getAllArticleTypes(user.getUserId());
		request.setAttribute("articleTypes", articleTypes);

		return "addArticle";
	}

	// 添加文章
	@RequestMapping("createArticle")
	public String createArticle(HttpServletRequest request, String articleTitle, String articleType,
			String articleContent, HttpSession session) {
		return articleService.createArticle(articleTitle, articleType, articleContent, request, session);
	}

	// 测试
	@RequestMapping("/indexMail")
	public String showIndex() {
		return "indexMail";
	}

	// 测试
	@RequestMapping("/mail")
	public String showMail() {
		return "mail";
	}

	// 显示文章
	@RequestMapping("/showArticle")
	public String showArticle(HttpServletRequest request, HttpSession session, long articleId) {
		Article article = articleService.getArticleByArticleId(articleId);
		request.setAttribute("article", article);
		return "showArticle";
	}

	// 删除文章
	@RequestMapping("deleteArticle")
	public String deleteArticle(HttpServletRequest request, HttpSession session, long articleId, long authorId) {
		return articleService.deleteArticleById(request, session, articleId, authorId);
	}

	// 跳转到博客更新页面
	@RequestMapping("updateArticleById")
	public String updateArticle(HttpServletRequest request, HttpSession session, long articleId, long authorId) {
		User user = userService.getUserById(authorId);
		Article article = articleService.getArticleByArticleId(articleId);
		request.setAttribute("article", article);
		session.setAttribute("user", user);
		return "editArticle";
	}

	// 更新博客
	@RequestMapping("editArticle")
	public String editArticle(HttpServletRequest request, HttpSession session, long articleId, long authorId,
			String articleTitle, String articleType, String articleContent) {

		int updateReust = articleService.updateArticleById(articleTitle, articleType, articleContent, articleId,
				authorId);
		if (updateReust != 1) {
			logger.info("更新文章出错");
			request.setAttribute("updateFailed", "更新文章出错！");
		} else {
			request.setAttribute("updateSucceed", "更新文章成功！");
		}
		List<Article> articles = articleService.getAllArticles(authorId);
		session.setAttribute("articles", articles);
		User user = userService.getUserById(authorId);
		session.setAttribute("user", user);
		return "userindex";
	}
	
	//根据类型获取博客
	@RequestMapping("getArticleByType")
<<<<<<< HEAD
	public String getArticleByType(HttpServletRequest request, HttpSession session) throws UnsupportedEncodingException {
=======
	public String getArticleByType(HttpServletRequest request, HttpSession session, String articleType) throws UnsupportedEncodingException {
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect: /user/login";
		}
		//转换编码以防传过来的参数出现中文乱码
<<<<<<< HEAD
		String articleType = new String(request.getParameter("articleType").getBytes("ISO8859_1"));
		
		logger.info("Input Parameter Article Type is : " + articleType);
		// get page
		String pageNow = request.getParameter("pageNow");
		logger.info("当前的page " + pageNow);

		Page page = null;//
		
		List<Article> totalArticles = articleService.getArticleByType(articleType);
		
		logger.info("文章总数为： " + totalArticles.size());

		// 当前也显示的文章列表
		List<Article> articles = new ArrayList<Article>();
		if (pageNow != null) {
			page = new Page(totalArticles.size(), Integer.parseInt(pageNow));
			// articles = articleService.getArticleByPage(page.getStartPos(),
			// page.getPageSize());
		} else {
			page = new Page(totalArticles.size(), 1);
			// articles = articleService.getArticleByPage(page.getStartPos(),
			// page.getPageSize());
		}

		articles = articleService.getArticleByPageAndType(page.getStartPos(), page.getPageSize(), user.getUserId(), articleType);
		session.setAttribute("page", page);
		session.setAttribute("articles", articles);
		session.setAttribute("user", user);
		request.setAttribute("articleType", articleType);
		return "showArticleByType";		
=======
		String type = new String(request.getParameter("articleType").getBytes("ISO8859_1"));
		
		logger.info("Input Parameter Article Type is : " + type);
		
		List<Article> articles = articleService.getArticleByType(type);
		session.setAttribute("articles", articles);
		session.setAttribute("user", user);
		return "userindex";		
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
	}
}
